package com.dragon.permission_service.Service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.permission_service.Exception.BusinessException;
import com.dragon.permission_service.Exception.StateCode;
import com.dragon.permission_service.Service.UserService;
import com.dragon.permission_service.domain.entity.User;
import com.dragon.permission_service.mapper.UserMapper;
import com.dragon.permission_service.util.JwtUtils;
import com.dragon.permission_service.util.ThrowUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
/**
* @author 20390
* @description 针对表【user(用户)】的数据库操作Service实现
* @createDate 2025-06-22 20:06:25
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Resource
    private UserMapper userMapper;
    @Autowired
    private JwtUtils jwtUtils;
    public User getLoginUser(HttpServletRequest request) {
        ThrowUtil.throwIf(ObjectUtil.isEmpty(request), StateCode.PARAMS_ERROR);
        String authorizationHeader = request.getHeader("Authorization"); // 获取Authorization头
        if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
            String jwtToken = authorizationHeader.substring(7); // 提取Token字符串
            // ... 验证Token
            User loginUser = jwtUtils.getUserOrNull(jwtToken);
            if (ObjectUtil.isEmpty(loginUser)) {
                throw new BusinessException(StateCode.PARAMS_ERROR, "用户未登录");
            }
            return loginUser;
        } else {
            throw new BusinessException(StateCode.PARAMS_ERROR, "用户未登录");
        }
    }
}




